﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace kalkulator
{

    //перенаправлять на равно если нажимаешь клавиши операций

    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        calkulators.arifm ar = new calkulators.arifm();
        calkulators.trigonom tr = new calkulators.trigonom();
        int key = 0; // 0 - продолжать ввод, 1 - начинать ввод с первой цифры
        string operant = "";

        public void ravnoo(string a)
        {
            if ((a == "sin") || (a == "cos") || (a == "tg") || (a == "ctg"))
            {
                tr.operant1 = ar.operant1;
                tr.rezult = ar.rezult;
                //tr.operant2 = ar.operant2;
                tr.ravnoTR(a);
                ar.operant1 = tr.operant1;
                ar.rezult = tr.rezult;
                //ar.operant2 = tr.operant2;

            }
            else ar.ravnoAR(a);
        }

        public void forTwo(string a)
        {
            if ((operant != "") && (key != 1))
            {
                button24_Click(null, null);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox1.Text = "ошибка";
                    operant = "";
                }
                else
                {
                    ar.operant1 = ar.rezult;
                    operant = a;
                }


                //ar.operant2 = Convert.ToDouble(textBox1.Text);
                //ar.ravno(operant);
                //if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                //{
                //    textBox1.Text = "ошибка";
                //    operant = "";
                //}
                ////textBox1.Text = Convert.ToString(ar.rezult);
                ////ar.operant1 = ar.rezult;
                ////if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                ////{
                ////    textBox1.Text = "ошибка";
                ////    operant = "";
                ////}
                //else
                //{
                //    ar.operant1 = ar.rezult;
                //    textBox1.Text = Convert.ToString(ar.rezult);
                //    operant = a;
                //}
                //key = 1;

            }

            else
            {
                if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
                {
                    ar.operant1 = Convert.ToDouble(textBox1.Text);
                    operant = a;
                }
                key = 1;
            }
        }

        public void forOne(string a)
        {
            if ((operant != "") && (key != 1))
            {
                button24_Click(null, null);
                operant = a;
                ar.operant1 = ar.rezult;
                ravnoo(a);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox1.Text = "ошибка";
                    operant = "";
                }
                else
                {
                    textBox1.Text = Convert.ToString(ar.rezult);
                    operant = "";
                }

            }
            else
            {
                if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
                {
                    ar.operant1 = Convert.ToDouble(textBox1.Text);
                    ravnoo(a);
                    textBox1.Text = Convert.ToString(ar.rezult);
                    //operant = a;
                    operant = "";

                }
                key = 1;
            }

        }


        public MainWindow()
        {

            InitializeComponent();
            textBox1.Text = "";


        }

        private void windowsFormsHost1_ChildChanged(object sender, System.Windows.Forms.Integration.ChildChangedEventArgs e)
        {

        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

        }
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button11_Click(object sender, RoutedEventArgs e)//7
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(7);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(7);
        }

        private void button3_Click(object sender, RoutedEventArgs e)//3
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(3);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(3);
        }

        private void textBox1_TextChanged_1(object sender, TextChangedEventArgs e)
        {

        }

        private void button17_Click(object sender, RoutedEventArgs e) //+
        {
            forTwo("+");

            //if ((operant != "") && (key == 0))
            //{
            //    ar.operant2 = Convert.ToDouble(textBox1.Text);
            //    ar.ravno(operant);
            //    textBox1.Text = Convert.ToString(ar.rezult);
            //    ar.operant1 = ar.rezult;
            //    key = 1;
            //    operant = "+";
            //}
            //else
            //{
            //    if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
            //    {
            //        operant = "+";
            //        ar.operant1 = Convert.ToDouble(textBox1.Text);
            //    }
            //    key = 1;
            //}
        }

        private void button9_Click(object sender, RoutedEventArgs e)//5
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(5);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(5);

        }

        private void button24_Click(object sender, RoutedEventArgs e)//=
        {
            if ((operant != "") && (key == 0))
            {
                ar.operant2 = Convert.ToDouble(textBox1.Text);
                if (operant != "")
                    ravnoo(operant);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox1.Text = "ошибка";
                    operant = "";
                }
                else textBox1.Text = Convert.ToString(ar.rezult);
                key = 1;
                operant = "";


                //ar.operant2 = Convert.ToDouble(textBox1.Text);
                //if ((ar.operant1 == 0) && (operant == "/"))
                //{
                //    textBox1.Text = "ошибка";
                //}
                //else
                //{
                //    if ((operant == "+") || (operant == "-")||(operant=="*")||(operant=="/")||(operant=="^"))
                //    {
                //        ar.ravno(operant);
                //        //ar.rezult = ar.rezult;
                //    }
                //    textBox1.Text = Convert.ToString(ar.rezult);
                //}

                //key = 1;
                //operant = "";           
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)//1
        {
            //if (textBox1.Text == "ошибка")
            //key = 1;
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(1);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(1);
        }

        private void button4_Click(object sender, RoutedEventArgs e)//2
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(2);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(2);
        }

        private void button5_Click(object sender, RoutedEventArgs e)//9
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(9);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(9);
        }

        private void button8_Click(object sender, RoutedEventArgs e)//4
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(4);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(4);
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)//6
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(6);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(6);
        }

        private void button10_Click(object sender, RoutedEventArgs e)//8
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(8);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(8);
        }

        private void button16_Click(object sender, RoutedEventArgs e)//-
        {
            forTwo("-");
            //if ((operant != "") && (key != 1))
            //{

            //    ar.operant2 = Convert.ToDouble(textBox1.Text);
            //    ar.ravno(operant);
            //    textBox1.Text = Convert.ToString(ar.rezult);
            //    ar.operant1 = ar.rezult;
            //    key = 1;
            //    operant = "-";
            //}

            //else
            //{
            //    if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
            //    {
            //        operant = "-";
            //        ar.operant1 = Convert.ToDouble(textBox1.Text);
            //    }
            //    key = 1;
            //}
        }

        private void button15_Click(object sender, RoutedEventArgs e)//*
        {
            forTwo("*");
            //if ((operant != "") && (key == 0))
            //{
            //    ar.operant2 = Convert.ToDouble(textBox1.Text);
            //    ar.ravno(operant);
            //    textBox1.Text = Convert.ToString(ar.rezult);
            //    ar.operant1 = ar.rezult;
            //    key = 1;
            //    operant = "*";
            //}
            //else
            //{
            //    if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
            //    {
            //        operant = "*";
            //        ar.operant1 = Convert.ToDouble(textBox1.Text);
            //    }
            //    key = 1;
            //}
        }

        private void button14_Click(object sender, RoutedEventArgs e)// \
        {
            forTwo("/");
        }

        private void button6_Click(object sender, RoutedEventArgs e)//0
        {
            if (key == 1)
            {
                textBox1.Text = Convert.ToString(0);
                key = 0;
            }
            else textBox1.Text = textBox1.Text + Convert.ToString(0);
        }

        private void button18_Click(object sender, RoutedEventArgs e)//C
        {
            key = 0;
            operant = "";
            ar.operant1 = 0;
            ar.operant2 = 0;
            tr.operant1 = 0;
            tr.operant2 = 0;
            tr.rezult = 0;
            ar.rezult = 0;
            textBox1.Text = "";
        }

        private void button19_Click(object sender, RoutedEventArgs e) //степень
        {
            forTwo("^");
            //if ((operant != "") && (key == 0))
            //{
            //    ar.operant2 = Convert.ToDouble(textBox1.Text);
            //    ar.ravno(operant);
            //    textBox1.Text = Convert.ToString(ar.rezult);
            //    ar.operant1 = ar.rezult;
            //    key = 1;
            //    operant = "^";
            //}
            //else
            //{
            //    if ((textBox1.Text != "ошибка") && (textBox1.Text != ""))
            //    {
            //        operant = "^";
            //        ar.operant1 = Convert.ToDouble(textBox1.Text);
            //    }
            //    key = 1;
            //}
        }

        private void button2_Click(object sender, RoutedEventArgs e)//корень
        {
            forOne("sqrt");
            //if ((operant != "") && (key == 0))
            //{
            //    ar.operant2 = Convert.ToDouble(textBox1.Text);
            //    ar.ravno(operant);
            //    ar.operant1 = ar.rezult;
            //    operant = "sqrt";
            //    //if (ar.operant1<0)
            //    //{
            //    ar.ravno(operant);
            //        //textBox1.Text = "ошибка";
            //    if ((ar.rezult == 0)&&(ar.operant1==-2)&&(ar.operant2==-1))
            //    {
            //        operant = "";
            //        textBox1.Text = "ошибка";
            //        key = 1;
            //    }
            //    //}
            //    else
            //    {
            //        operant = "sqrt";
            //        textBox1.Text = Convert.ToString(ar.rezult); ;
            //    }
            //    //textBox1.Text = Convert.ToString(ar.rezult);      
            //    //key = 1;
            //}
            //else
            //{
            //    if (textBox1.Text != "ошибка")
            //    {
            //        operant = "sqrt";
            //        ar.operant1 = Convert.ToDouble(textBox1.Text);
            //        ar.ravno(operant);
            //        textBox1.Text = Convert.ToString(ar.rezult);
            //        //ar.operant1 = Convert.ToDouble(textBox1.Text);
            //        operant = "";
            //    }
            //    key = 1;
            //}
        }

        private void button23_Click(object sender, RoutedEventArgs e)//sin
        {
            forOne("sin");

        }

        private void button22_Click(object sender, RoutedEventArgs e) //cos
        {
            forOne("cos");
        }

        private void button21_Click(object sender, RoutedEventArgs e) //tg
        {
            forOne("tg");
        }
        private void button20_Click(object sender, RoutedEventArgs e)//ctg
        {
            forOne("ctg");
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            if ((textBox1.Text != "ошибка") && (textBox1.Text != "") && (key == 0))
                textBox1.Text = textBox1.Text + ",";
        }


        private void MenuItem_Click_1(object sender, RoutedEventArgs e) //алгебр
        {
            System.Diagnostics.Process.Start("Вычисление алгебраических операций.txt");
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("Вычисление тригонометрических функций.txt");
        }

    }
}
